<?php
/* 
 *reund
 */
class RefundAction extends CommonAction {
    public function _filter(&$map) {
        $key = trim($_REQUEST['keywords']);
        if(!empty($key)) {
            //$map['name'] = array('like',"%".$key."%");//产品名称
            $map['buy_no'] = array('like',"%".$key."%");
        }
        $sid = $_REQUEST['supplier_id'];
        if(!empty($sid))
            $map['supplier_id'] = $sid;
    }


   //退款处理 
    public function add(){
        if(empty( $_POST['goods_id'] )){
            $sd = M('Sale_detail')->where('sale_id = '.$_GET['id'])->findAll();
           $this->assign('sdetails',$sd);
           $this->edit('Sale','add');    
        }else{
           $data = $_POST;
           $count_refund = count($_POST['refund']);
           $sum_refund = 0 ; //sum refund
           for($i=0;$i < $count_refund;$i++){
               if($data['refund'][$i] == 0 ) //退款为0的则不记录
                   continue;
              $_POST = array(
                'sale_id' => $data['sale_id'][$i],
                'mall_no' => $data['mall_no'][$i],
                'order_source_id' => $data['order_source_id'][$i],
                'goods_id' => $data['goods_id'][$i],
                'number' => $data['number'][$i],
                'reason' => $data['reason'][$i],
                'remark' => $data['remark'][$i],
                'refund_time' => $data['refund_time'][$i],
                'refund_type' => $data['refund_type'][$i],
                'user_id' => $_SESSION[C('USER_AUTH_KEY')],
                'refund' => $data['refund'][$i],
                );
              $this->micro_insert('Refund');
              unset($_POST);
              $sum_refund += $data['refund'][$i]; 
              //TODO sum refund; recalculate sale real_pay,and change sale order status
           }
           M('Sale')->setDec('real_pay','id = '.$data['sale_id']['0'],$sum_refund);

           $this->success("新增退款成功!"); 
        }
    }

   public function _before_update(){
      //TODO 如果退款金额变动,则重新计算订单的 实付款     
   } 
}
?>
